package com.jb.repair.acticity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.dao.Db;
import com.jb.repair.model.QxjlModel;
import com.jb.repair.model.SbbdzdyModel;
import com.jb.repair.util.DownAttachmentDialogUtil;
import com.jb.repair.util.DownFileAsyncTask;
import com.jb.repair.util.IntentUtils;
import com.jb.repair.util.StringUtils;
import com.jb.repair.view.InfoTextView;
import com.jb.repair.view.TitleView;

import java.io.File;
import java.util.List;

/**
 * 缺陷记录详情页
 */
public class BugRecordActivtity extends BaseActivity {

    private TitleView titleView;
    private InfoTextView tv_data, tv_grade, tv_type, tv_people, tv_bug_info,
            tv_report;
    private Db db;
    private String bugLevel = "";

    private LinearLayout ll_qxjl;
    private InfoTextView tv_source_id, foundman_id, tv_defect_status, tv_solved_time_editable, tv_solved_result, tv_note;
    private static final String TAG = "BugRecordActivtity";
    private QxjlModel model;
    private int i = -1;
    private DownAttachmentDialogUtil dialogUtil;


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qxjlxq);
        initviews();
        db = Db.getInstance(this);
        String form_CODE = db.getName2("CODE", "TB_SYS_CODE", "CODE_NAME", "缺陷记录");
        Log.e("1231", form_CODE);
        String id_zhuanye = getIntent().getStringExtra("id_zhuanye");
        Log.e("1231", id_zhuanye);

        model = (QxjlModel) getIntent().getSerializableExtra("qxjl");

//        int i = model.defect_time.lastIndexOf(".");
//        if (-1 != i){
//            tv_data.setText(model.defect_time.substring(0, i));
//        }else{
        tv_data.setText(model.found_time);
//        }

//        Log.e("BBB",level+3);

        String level = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.level_id);
        tv_grade.setText(level);

        String type = db.getName("defect_type", "TB_EQ_DEFECT_TYPE", "guid", model.type_id);
        tv_type.setText(type);

        String visible = db.getVisible(form_CODE, "solvedman_id_editable");
        Log.e("1231", "solvedman_id_editable = " + visible);
        if (!visible.equals("T")) {
            tv_people.setVisibility(View.GONE);
        }
        String name = db.getName("PERS_NAME", "TB_SYS_PERSON", "GUID", model.solvedman_id_editable);
        tv_people.setText(name);

        tv_bug_info.setText(model.defect_describe);
        String name2 = db.getName("WJMC", "TB_FILE_LB", "GUID", model.attachment);
        Log.e("1111", model.attachment);
        if (!StringUtils.isNullOrEmpty(name2)) {
            tv_report.setText(name2);
            i = 1;
            Log.e("1111", "GUID");
        } else {
            String name5 = db.getName("WJMC", "TB_FILE_LB", "OBJ_ID", model.attachment);
            tv_report.setText(name5);
            i = 2;
            Log.e("1111", "OBJ_ID");
        }


        String source_id = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.source_id);
        tv_source_id.setText(source_id);
        String name1 = db.getName("PERS_NAME", "TB_SYS_PERSON", "GUID", model.foundman_id);

        foundman_id.setText(name1);

        tv_defect_status.setText(model.defect_status);
        String visible1 = db.getVisible(form_CODE, "solved_time_editable");
        Log.e("1231", "solved_time_editable = " + visible1);
        if (!visible1.equals("T")) {
            tv_solved_time_editable.setVisibility(View.GONE);
        }
        tv_solved_time_editable.setText(model.solved_time_editable);

        tv_solved_result.setText(model.solved_result);
        String inspect_id = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.inspect_id);
        String patrol_id = db.getName("CODE_NAME", "TB_SYS_CODE", "CODE", model.patrol_id);

        tv_note.setText(model.note);

        //----------------------------------------------处理自定义字段逻辑-----------------------------------------------------------
        //查找自定义字段显示
        //拿到表单编码
        List<SbbdzdyModel> sbbdzdyModelList = db.getSbbdzdyModel(form_CODE, id_zhuanye);
        Log.e("1231", String.valueOf(sbbdzdyModelList.size()));

        for (SbbdzdyModel model1 : sbbdzdyModelList) {
            Log.e("1231", "是否显示" + model1.is_show_eqbook);
            if (model1.is_show_eqbook.equals("T")) {
                InfoTextView infoTextView = new InfoTextView(this);
                Log.e("1231", "需要动态添加条目");
                //需要设置key的值
                infoTextView.setKey(model1.cus_name);
                ll_qxjl.addView(infoTextView);

                //设置value的值
                String format = model1.field_format;
                String value = db.getZdyzdValue(model1.guid, model.guid);
                if (value == null) {
                    value = "";
                } else if (format != null && "时间".equals(format) && value.length() > 10) {
                    value = value.substring(0, 10);
                }
                infoTextView.setText(value);
//                String zdyzdValue = db.getZdyzdValue(model1.guid, model.guid);
//                infoTextView.setText(zdyzdValue);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = (int) getResources().getDimension(R.dimen.ledger_info_textsize);
                infoTextView.setLayoutParams(params);
            }

        }
//-------------------------------------------------------------------------------------------------------

        fillFakeData();
    }

    private void initviews() {
        titleView = (TitleView) this.findViewById(R.id.titleView);
        tv_data = (InfoTextView) findViewById(R.id.tv_data);
        tv_grade = (InfoTextView) findViewById(R.id.tv_grade);
        tv_people = (InfoTextView) findViewById(R.id.tv_people);
        tv_bug_info = (InfoTextView) findViewById(R.id.tv_bug_info);
        tv_type = (InfoTextView) findViewById(R.id.tv_type);
        tv_report = (InfoTextView) findViewById(R.id.tv_report);
        tv_source_id = (InfoTextView) findViewById(R.id.tv_source_id);
        foundman_id = (InfoTextView) findViewById(R.id.foundman_id);
        tv_defect_status = (InfoTextView) findViewById(R.id.tv_defect_status);
        tv_solved_time_editable = (InfoTextView) findViewById(R.id.tv_solved_time_editable);
        tv_solved_result = (InfoTextView) findViewById(R.id.tv_solved_result);
        tv_note = (InfoTextView) findViewById(R.id.tv_note);
        ll_qxjl = (LinearLayout) findViewById(R.id.ll_qxjl);


        titleView.setOnClickListener(new TitleView.OnTitleViewClickListener() {
            @Override
            public void onLeftImageClick(View view) {
                super.onLeftImageClick(view);
                finish();
            }
        });
    }

    /**
     * 填充假数据
     */
    private void fillFakeData() {
        tv_report.setOnClickListener(new View.OnClickListener(
        ) {
            public void onClick(View view) {
                Log.i("点击测试", "收到点击事件");
                //附件的guid
                final String guid;
                //附件的文件类型
                final String fjlx;
                if (i == 1) {
                    guid = db.getName("GUID", "TB_FILE_LB", "GUID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "GUID", model.attachment);
                } else {
                    guid = db.getName("GUID", "TB_FILE_LB", "OBJ_ID", model.attachment);
                    fjlx = db.getName("LX", "TB_FILE_LB", "OBJ_ID", model.attachment);
                }
                //判断是否有附件
                boolean nullOrEmpty = StringUtils.isNullOrEmpty(tv_report.getText());
                Log.e("LLL", String.valueOf(nullOrEmpty));
                if (nullOrEmpty) {
                    return;
                } else {
                    //有附件
                    String filePath = AppConfig.ATTACHMENT_PATh + guid + "." + fjlx;
                    Log.e("1111", filePath);
                    File file = new File(filePath);
                    if (!file.exists()) {
                        Toast.makeText(BugRecordActivtity.this, "文件不存在", Toast.LENGTH_SHORT).show();
                        dialogUtil = new DownAttachmentDialogUtil(BugRecordActivtity.this, R.style.myDialog,
                                new DownAttachmentDialogUtil.AttachDialogListener() {
                                    @Override
                                    public void onClick(View v) {
                                        switch (v.getId()) {
                                            case R.id.attach_dialog_canlebtn:
                                                dialogUtil.dismiss();
                                                break;
                                            case R.id.attach_dialog_surebtn:
                                                new DownFileAsyncTask(BugRecordActivtity.this, guid, model.attachment, fjlx).execute();


                                                dialogUtil.dismiss();

                                                break;
                                        }
                                    }

                                });
                        Window win = dialogUtil.getWindow();
                        win.setGravity(Gravity.CENTER);
                        dialogUtil.show();
                    } else {
                        IntentUtils.JumpToIntent(BugRecordActivtity.this, fjlx, filePath);

                    }

                }
            }

        });
    }
}
